package com.tqjc.execute.work;
import com.tqjc.execute.utils.PublicResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 天车工作工厂类，负责匹配工作对象
 * @author: pzy
 * @date: 2023年05月19日 11:06
 */
@Component
@Slf4j
public class CraneWorkFactory {

    private Map<String, CraneWork> craneWorkMap = new LinkedHashMap<>(8);

    /**
     * create by: pzy
     * description: 构造方法，自动注入天车的实现类
     * create time: 2023/5/19 11:12
     *
     * @return
     * @param: [craneWorks]
     */
    public CraneWorkFactory(List<CraneWork> craneWorks) {
        for (CraneWork craneWork : craneWorks) {
            craneWorkMap.put(craneWork.getCraneType(), craneWork);
        }
    }

    /**
     * create by: pzy
     * description: 获取对应天车的实现类
     * create time: 2023/5/19 11:12
     *
     * @return com.tqjc.execute.utils.PublicResult<com.tqjc.execute.work.CraneWork>
     * @param: [craneType]
     */
    public PublicResult<CraneWork> getCraneWorkImpl(String craneType) {
        if (craneWorkMap.containsKey(craneType)) {
            return PublicResult.ok(craneWorkMap.get(craneType));
        } else {
            log.error("没有对应得工作天车类型：{}", craneType);
            return PublicResult.fail("没有对应得工作天车类型。");
        }
    }
}
